package friot.service.cloud.service;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import friot.model.iot.entity.ContactProductModel;
import friot.model.iot.entity.IotModel;
import friot.model.iot.entity.IotProduct;
import friot.service.cloud.mapper.ContactProductModelMapper;
import friot.service.cloud.mapper.IotProductMapper;
import friot.utils.constants.AppHttpCodeEnum;
import friot.utils.exception.ServiceException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author He Kuntao
 * @desc IoT Product Service
 * @date 2024/2/19 11:24
 */
@Service("iotProductService")
public class IotProductServiceImpl extends ServiceImpl<IotProductMapper, IotProduct> implements IotProductService {

    @Resource
    private ContactProductModelMapper contactProductModelMapper;
    @Resource
    private ContactProductModelService contactProductModelService;
    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 查询所有产品原型
     *
     * @param status 是否关联status
     * @return 产品原型集合
     */
    @Override
    public List<IotProduct> listProduct(Boolean status) {
        return lambdaQuery().eq(status, IotProduct::getStatus, 1).list();
    }

    /**
     * 保存产品原型
     *
     * @param iotProduct 产品原型
     */
    @Override
    public void saveProduct(IotProduct iotProduct) {
        // 参数校验
        if (StringUtils.isBlank(iotProduct.getProductName())) {
            throw new ServiceException(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        // 暂时设置固定图片
        iotProduct.setImgUrl("http://101.33.237.12/static/img/gateway.5a51e30f.png");
        this.save(iotProduct);
    }

    /**
     * 绑定数据模型
     *
     * @param productId 产品原型ID
     * @param modelList 数据模型ID集合
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void bindModel(Integer productId, ArrayList<Integer> modelList) {
        /* 参数校验 */
        if (modelList.isEmpty()) {
            throw new ServiceException(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        /* 关联模型数据 */
        modelList.forEach(modelId -> {
            ContactProductModel model = new ContactProductModel();
            model.setProductId(productId);
            model.setModelId(modelId);
            contactProductModelService.save(model);
        });

        /* 设置产品已管理状态 */
        IotProduct iotProduct = new IotProduct();
        iotProduct.setProductId(productId);
        iotProduct.setStatus("1");
        this.updateById(iotProduct);

        /* 发送模型绑定信息到RabbitMQ，消费者将模型异步存入Redis */
        rabbitTemplate.convertAndSend("amq.fanout","",productId);
    }

    /**
     * 查询绑定的数据模型
     *
     * @param productId 产品原型ID
     * @return 数据模型集合
     */
    @Override
    public List<IotModel> getBindModels(Integer productId) {
        return contactProductModelMapper.listBindModels(productId);
    }
}
